/**
 * 
 */
package ar.edu.unq.svnpermission.ui.pages;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import ar.edu.unq.svnpermission.domain.Folder;
import ar.edu.unq.svnpermission.ui.exceptions.FolderAlreadyExist;
import ar.edu.unq.svnpermission.ui.model.Folders;
import ar.edu.unq.svnpermission.ui.model.NewFolder;

/**
 * Contiene la lógica de la Página de creación de Carpetas
 * @author nicolas
 */
public class NewFolderPage extends InternalPage<NewFolder> {

	private static final long serialVersionUID = 1L;
	public final static String FORM = "newFolderForm";
	private Folder parentFolder;
	
	public NewFolderPage()
	{
		this.setParentFolder(new Folders().getRootFolder());
	}
	
	public NewFolderPage(Folder parentFolder)
	{
		this.setParentFolder(parentFolder);
		((NewFolder)NewFolderPage.this.getForm().getDefaultModelObject()).setParentFolder(parentFolder);
	}
	
	
	
	@Override
	protected void addFields() {
		//Agrego los componentes
		this.getForm().add(new TextField<String>("name"));
		this.getForm().add(new TextArea<String>("comments"));		
		this.getForm().add(new FeedbackPanel("errorPanel"));		
	}

	@Override
	protected void addButtons() {
		//Agrego el boton de Creación
		this.getForm().add(new Button("create") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				try {
					//Implemento la logica de negocios del boton "Crear"
					((NewFolder)NewFolderPage.this.getForm().getDefaultModelObject()).create();
					this.setResponsePage(new PermissionManagerPage());
				}
				catch (FolderAlreadyExist e) {
					//Muestro en el panel de error el mensaje de la excepcion
					NewFolderPage.this.getForm().get("errorPanel").error(e.getMessage());
				}
			}
		});				
	}

	@Override
	protected CompoundPropertyModel<NewFolder> createModel() {
		return new CompoundPropertyModel<NewFolder>(new NewFolder(this.getParentFolder()));
	}

	@Override
	protected String getFormName() {
		return NewFolderPage.FORM;
	}

	public void setParentFolder(Folder parentFolder) {
		this.parentFolder = parentFolder;
	}

	public Folder getParentFolder() {
		return parentFolder;
	}

}
